package com.k.base.jdk.thread.future;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import java.util.concurrent.*;

/**
 * 1.可以接收 上一部结果，进行下一步运算
 *
 * @author kk
 */
public class CompleteFutureTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService pool = Executors.newFixedThreadPool(6);
        // 变换结果
        String result = CompletableFuture
                .supplyAsync(() -> {
                            return "Hello ";
                        }
                        , pool)
                .thenApplyAsync(v -> v + "world")
                .join();
        System.out.println(Thread.currentThread() + ":" + result);


        // 消费结果

        CompletableFuture<String> rr1 = CompletableFuture
                .supplyAsync(() -> {
                    String myResult ="";
                    try {
                        System.out.println("rr1 thread ss");
                        Thread.sleep(4000);
                        System.out.println("rr1 thread ee");
                        myResult ="Hello ";
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("rr1 thread end"+result);
                    return myResult;
                })
//                .thenAccept(v -> {
//                    System.out.println("consumer: " + v);
//                })
                ;

//        CompletableFuture<String> rr2 =CompletableFutureTimeout.completeOnTimeout("no", rr1, 2000, TimeUnit.MILLISECONDS);
        CompletableFuture<String> rr2 = CompletableFutureTimeout.timeoutAfter(2000,TimeUnit.MILLISECONDS);
        System.out.println("rr2.get() start:" );
        System.out.println("rr2.get()"+ rr2.get());
        System.out.println("rr2.get() end");
        System.out.println("==============================nono");

        Thread.sleep(999999999);

        // 返回最快的Sep
        String result2 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "Hello";
        }).thenCombine(CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "world";
        }), (s1, s2) -> {
            return s1 + " " + s2;
        }).join();
        System.out.println("result2:" + result2);

        // 需要上一阶段的返回值，并且other代表的CompletionStage也要返回值之后，把这两个返回值，进行转换后返回指定类型的值。
        String result3 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(400);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "Hi Boy";
        }).applyToEither(CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "Hi Girl";
        }), (s) -> {
            return s;
        }).join();
        System.out.println("result3:" + result3);

        // Sep1运行异常，Sep2 异常补偿方法
        String result4 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (true) {
                throw new RuntimeException("exception test!");
            }

            return "Hi Boy";
        }).exceptionally(e -> {
            System.out.println(e.getMessage());
            return "Hello world!";
        }).join();
        System.out.println("result4:" + result4);
    }
}
